package com.jzx.controller;

import com.google.gson.Gson;
import com.jzx.bean.UserMessage;
import com.jzx.service.UserMessageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

/**
 * @author JunedQQ
 * @create 2019/6/30
 * @since 1.0.0
 */
@Controller
public class UserMessageController {

    @Autowired
    private UserMessageService userMessageService;

    private Gson gson = new Gson();
    public static final String SERVER_ERROR = "服务器错误";
    public static final String SERVER_SUCCESS = "success";
    public static final String SERVER_FAILURE = "failure";
    public static final String CHARACTER_ENCODING = "UTF-8";
    public static final String NULL = "";

    @RequestMapping(path = "/getUserMessageById")
    public void getUserMessageById(HttpServletResponse response, String id) {
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            UserMessage userMessage = userMessageService.getUserMessageById(id);
            //转化为json数据
            String result = gson.toJson(userMessage);
            //返回数据
            pw.write(result);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/getUserMessageByUserId")
    public void getUserMessageByUserId(HttpServletResponse response, String userId) {
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            UserMessage userMessage = userMessageService.getUserMessageByUserId(userId);
            //转化为json数据
            String result = gson.toJson(userMessage);
            //返回数据
            pw.write(result);
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }


    @RequestMapping(path = "/updateUserMessageEmailById")
    public void updateUserMessageEmailById(HttpServletResponse response, String id, String newEmail) {
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userMessageService.updateUserMessageEmailById(id, newEmail);
            //判断是否修改成功
            if (result == 1) {
                pw.write(SERVER_SUCCESS);
            } else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }


    @RequestMapping(path = "/updateUserMessageEmailByUserId")
    public void updateUserMessageEmailByUserId(HttpServletResponse response, String userId, String newEmail) {
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userMessageService.updateUserMessageEmailByUserId(userId, newEmail);
            //判断是否修改成功
            if (result == 1) {
                pw.write(SERVER_SUCCESS);
            } else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/updateUserMessageNickNameById")
    public void updateUserMessageNickNameById(HttpServletResponse response, String id, String newNickName) {
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userMessageService.updateUserMessageNickNameById(id, newNickName);
            //判断是否修改成功
            if (result == 1) {
                pw.write(SERVER_SUCCESS);
            } else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/updateUserMessageNickNameByUserId")
    public void updateUserMessageNickNameByUserId(HttpServletResponse response, String userId, String newNickName) {
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userMessageService.updateUserMessageNickNameByUserId(userId, newNickName);
            //判断是否修改成功
            if (result == 1) {
                pw.write(SERVER_SUCCESS);
            } else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/updateUserMessageSignatureById")
    public void updateUserMessageSignatureById(HttpServletResponse response, String id, String newSignature) {
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userMessageService.updateUserMessageSignatureById(id, newSignature);
            //判断是否修改成功
            if (result == 1) {
                pw.write(SERVER_SUCCESS);
            } else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/updateUserMessageSignatureByUserId")
    public void updateUserMessageSignatureByUserId(HttpServletResponse response, String userId, String newSignature) {
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userMessageService.updateUserMessageSignatureByUserId(userId, newSignature);
            //判断是否修改成功
            if (result == 1) {
                pw.write(SERVER_SUCCESS);
            } else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/updateUserMessageSexById")
    public void updateUserMessageSexById(HttpServletResponse response, String id, String sex){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userMessageService.updateUserMessageSexById(id, sex);
            //判断是否修改成功
            if (result == 1) {
                pw.write(SERVER_SUCCESS);
            } else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/updateUserMessageSexByUserId")
    public void updateUserMessageSexByUserId(HttpServletResponse response, String userId, String sex){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userMessageService.updateUserMessageSexByUserId(userId, sex);
            //判断是否修改成功
            if (result == 1) {
                pw.write(SERVER_SUCCESS);
            } else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/addUserMessage")
    public void addUserMessage(HttpServletResponse response,UserMessage userMessage){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userMessageService.addUserMessage(userMessage);
            //判断是否修改成功
            if (result == 1) {
                pw.write(SERVER_SUCCESS);
            } else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/deleteUserMessageById")
    public void deleteUserMessageById(HttpServletResponse response,String id){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userMessageService.deleteUserMessageById(id);
            //判断是否修改成功
            if (result == 1) {
                pw.write(SERVER_SUCCESS);
            } else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    @RequestMapping(path = "/deleteUserMessageByUserId")
    public void deleteUserMessageByUserId(HttpServletResponse response,String userId){
        //设置字符编码
        response.setCharacterEncoding(CHARACTER_ENCODING);
        //创建输出对象
        PrintWriter pw = null;
        try {
            //获取输出对象
            pw = response.getWriter();
            //查询用户
            int result = userMessageService.deleteUserMessageByUserId(userId);
            //判断是否修改成功
            if (result == 1) {
                pw.write(SERVER_SUCCESS);
            } else {
                pw.write(SERVER_FAILURE);
            }
        } catch (Exception e) {
            pw.write(SERVER_ERROR);
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

}